Sentdex ! Mayday! Mayday!

by: PyLORD, 7 years ago


Hello Sentdex, I have a problem with some of the code in your PyGame tutorials for Bucky on Youtube in the 9th tutorial. The problem is that when I run the code no errors are reported and the window automatically closes:

This is the code:

import pygame

width, height = 800, 600

gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption('Slither')

fps  = 15
clock = pygame.time.Clock()

# Colours
white = (255, 255, 255)
black = (0, 0 , 0)
red = (255, 0,  0)
yellow = (220, 120, 23)

lead_x = width / 2
lead_y = height / 2
lead_x_change = 0
lead_y_change = 0

block_size = 10

gameExit = False
# Main game loop
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
lead_x_change = block_size
lead_y_change = 0
elif event.key == pygame.K_UP:
lead_y_change = -block_size
lead_x_change = 0
elif event.key == pygame.K_DOWN:
lead_y_change = block_size
lead_x_change = 0

if lead_x >= width or lead_x < height or lead_y >= width or lead_y < height:
gameExit = True

lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size])
pygame.display.update()
clock.tick(fps)
pass

pygame.quit()
quit()



Thanks for your help.



You must be logged in to post. Please login or register an account.



Okay, so you just gotta think your way though this.

What are the reasons for the game to close? Maybe an error, but there doesn't seem to be one. Why else would the game close? The only other reason for the game to close is we get kicked outta this While loop. Why might that happen? Well, your code has one place only where this could happen.

...so start there. You could test by just simply commenting out, or removing that line, just to see if it is indeed the culprit. From there, start working through the logic of that line if that's the problem.

-Harrison 7 years ago

You must be logged in to post. Please login or register an account.

Check your 'x's and 'y's :-)

-scholzie 7 years ago

You must be logged in to post. Please login or register an account.